Repository navigation
fix(opencode): skip claude-code mirror rows already counted by Claude Code logs - #444
ilker-sahin wants to merge 4 commits into
Conversation
|
Two cases need fixes before merge. [P2] Preserve usage when the Claude transcript is absent With the OpenChamber The unconditional provider filter then removes the only available usage. A production-parser fixture with one 210-token OpenCode assistant row and no Claude transcript produced:
Expected: retain those 210 tokens. Please suppress a mirror only when corresponding Claude usage is available, or retain a fallback when its transcript is unavailable. The integration condition was established from source; no live paid API request was made. Source: LocalAdditionalUsageProvider.swift:390 (also applies to the second parser guard below). [P2] Rebase the saved counter before new native OpenCode usage Seed an existing installation on the same date with Claude The production |
… Code logs OpenCode wrappers such as OpenChamber run Claude Code through a claude-code provider and mirror every request into opencode.db. The Claude Code provider already counts that usage from its own logs, so the OpenCode reader counted each token twice. Skip rows with providerID claude-code in both the V2 session_message and legacy message parsers. Fixes chattymin#443.
Dropping every claude-code row removed the only record of usage when Claude Code did not persist a transcript (persistSession false, CLAUDE_CODE_SKIP_PROMPT_HISTORY). A mirror row is now dropped only when Claude Code has usage inside its time window; otherwise it counts. Unmatched rows younger than a grace period are held back until the transcript is flushed, and the incremental watermark stops before them. When filtering leaves OpenCode with no usage for the day, report an explicit zero so the companion ledger rebases its saved baseline before new native usage is credited.
982e50e to
3325bdd
Compare
…; log the defect class Adds tests for the match window boundaries, a pending mirror row resolved on a later scan behind a newer native row, and the scan resuming after dropped mirror rows. Records the wrapper double-count class in docs/reference/defect-log.md.
|
Updated - just putting here in case you hadn't seen. Thanks. |
Summary
Fixes #443.
When Claude Code runs through an OpenCode-based wrapper such as OpenChamber, the wrapper mirrors every request into
opencode.dbwithproviderID = "claude-code". Claude Code's own logs already count that usage, so the OpenCode provider counted each token a second time (on the reporter's machine, ~216M "OpenCode" tokens against ~220M Claude Code tokens for the same day, ~98% of it mirror rows).Mirror rows are dropped only when Claude Code usage covers them.
claude-coderow (V2session_messageand legacymessage) is matched by time: it is dropped if Claude Code has at least one usage entry inside the row's window (created - 5s…completed + 30s, both edges inclusive). A mirror row has no Claude message id, so a time window is the only link available.CLAUDE_CODE_SKIP_PROMPT_HISTORY=1,persistSession: false), the row counts as OpenCode usage, so it is not lost.LocalUsageCache.claudeEntriesonly on scans that actually met mirror rows. Users with no mirror rows get the same entries and watermark as before.Saved counter. When every OpenCode row for the day was a mirror, the provider reports an explicit zero for today (
LocalOpenCodeProvider.fetchDaily) instead of no data. The companion ledger then rebases that provider's saved baseline to 0 before new native usage arrives, so the first native tokens after upgrading are credited and old usage is not replayed.The mirrored provider IDs live in one
mirroredProviderIDsset inside the OpenCode reader, so no provider literal is added to a generic path. OpenCode entries are held in memory only (not inusage-cache.json), so no cache or parser version bump is needed. The defect class is recorded indocs/reference/defect-log.md.Known limits (deliberate):
Type of change
UI changes
No UI code changes. The only visible effect is that the OpenCode total (and the combined total) is lower for users who run Claude Code through OpenCode, and the zero row described above.
Checklist
swift buildandswift testpass (CIbuild-testpassed on this revision; not run locally, see Validation)Validation
New tests in
LocalAdditionalUsageTests: covered vs uncovered mirror rows (V2 and legacy); the no-transcript case (a single 210-token row is kept); the window edges; the grace period; a pending row that a later cache scan resolves behind a newer native row, without double counting; the scan resuming after dropped rows; the mirrored-days tracking; and the explicit zero.CompanionTestsdocuments the saved-baseline case. I broke the match, the pending-row watermark pin and the watermark over dropped rows on purpose to confirm each guard fails.I can't run
swift build/swift testlocally (Command Line Tools only, no Xcode), so CI is the test run, and it passes. Before pushing I compiled the realSources/PokeTokenBar/Corefiles withswiftcand ran theLocalAdditionalUsageTestsfile through a small XCTest stand-in, and ran a standalone probe against the realCompanionStorethat reproduces the reviewer's scenario (key absent: first native 100 credited as 0; explicit zero: credited as 100). On real data from the reporter's machine, all 121 mirror rows for the day were covered by Claude Code usage and none were kept; every mirror row that carries tokens also hastime.completed.